package com.jade.inventory.service.service.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.customer.Customer;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.zsy.inventory.dao.InventoryLogDao;
import com.jade.zsy.inventory.entity.Inventory;
import com.jade.zsy.inventory.entity.InventoryLog;
import com.jade.zsy.inventory.service.InventoryLogFacade;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * 库存变动日志
 * User: jinya
 * Date: 2017/5/24
 * Time: 15:41
 * TEL :17701640710   MAIL:2009_jinya@163.com
 * To change this template use File | Settings | File Templates.
 */
@Service("InventoryLogFacade")
public class InventoryLogFacadeImpl extends BaseFacadeImpl implements InventoryLogFacade {
    @Inject
    @Named("InventoryLogDao")
    private InventoryLogDao entityDao;

    @Override 
    public PagingResult queryEntity(Object obj, int page, int size, String keys, String... orders) throws DbException,IllegalAccessException, InstantiationException {
        Customer customer= (Customer) SecurityUtils.getSubject().getPrincipal();
        InventoryLog entity=(InventoryLog)obj;
        Criteria criteria = new Criteria((page - 1) * size, size);
        criteria.add(Restrictions.eq("ownerId",customer.getOwnerId()));
        if(StringUtils.isNotEmpty(keys)){
            criteria.add(Restrictions.or(Restrictions.like("phone",keys), Restrictions.eq("name",keys)));
        }
        PagingList pagingList = entityDao.queryEntity(Inventory.class, criteria);
        PagingResult result = PagingResult.getResult(CopyBean.copy(pagingList, InventoryLog.class), pagingList.getTotalCount(), page, size);
        return result;
    }
}